﻿using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
//using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
//using DG;
//using DG.Tweening;
//using Common;
using SimpleJson;

public class Utl : MonoBehaviour
{
    private string url = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll";
    public static Utl instance;

    public string path;
    public Coroutine c;
    void Awake()
    {
        instance = this;
        if (!Directory.Exists(Application.persistentDataPath + "/NoNameImg/"))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/NoNameImg/");
        }
        path = Application.persistentDataPath + "/NoNameImg/";
    }

    public void DestroyChip(GameObject obj)
    {
        Destroy(obj, 1);
    }

    public void stop()
    {
        StopAllCoroutines();
    }

    //public void GetCity(Action<string> cb)
    //{
    //    StartCoroutine(Request(cb));
    //}

    //IEnumerator Request(Action<string> cb)
    //{
    //    WWW www = new WWW(url);
    //    yield return www;

    //    if (string.IsNullOrEmpty(www.error))
    //    {
    //        Debug.Log(www.text);
    //        ResponseBody req = JsonConvert.DeserializeObject<ResponseBody>(www.text);
    //        cb(req.content.address_detail.city);
    //    }
    //}


    public Coroutine WaitForSec(float sec, Action cb)
    {
        return StartCoroutine(WaitForSec1(sec, cb));
    }

    IEnumerator WaitForSec1(float sec, Action cb)
    {
        yield return new WaitForSeconds(sec);
        cb();
    }

    public Coroutine WaitForTrue(Func<bool> fc, Action cb)
    {
        return StartCoroutine(WaitForTrue1(fc, cb));
    }

    IEnumerator WaitForTrue1(Func<bool> fc, Action cb)
    {
        while (true)
        {
            yield return null;
            if (fc()) break;
        }
        cb();
    }

    public void WaitForFrame(int num, Action cb)
    {
        StartCoroutine(WaitForFrame1(num, cb));
    }

    IEnumerator WaitForFrame1(int num, Action cb)
    {
        for (int i = 0; i < num; i++)
        {
            yield return new WaitForEndOfFrame();
        }
        cb();
    }



    public void WaitForLoop(int sec, Action<int> cbp)
    {
        StartCoroutine(WaitForLoop1(sec, cbp));
    }

    IEnumerator WaitForLoop1(int sec, Action<int> cbp)
    {
        for (int i = 0; i < sec; i++)
        {
            cbp(sec - i);
            yield return new WaitForSeconds(1);
        }
        cbp(0);
    }

    public GameObject PlaySound(string clipName, bool isLoop, float volume, bool is3D, float maxDis, Vector3 pos, Transform parent = null)
    {
        GameObject go = new GameObject(clipName + "Audio");
        AudioCtrl ac = go.AddComponent<AudioCtrl>();
        ac.asr = go.AddComponent<AudioSource>();
        ac.asr.rolloffMode = AudioRolloffMode.Linear;
        ac.SetInfo(clipName, isLoop, volume, is3D, maxDis);
        go.transform.parent = parent;
        return go;
    }

    public void SetLayer(string layerName, GameObject target)
    {
        target.layer = LayerMask.NameToLayer(layerName);
        for (int i = 0; i < target.transform.childCount; i++)
        {
            SetLayer(layerName, target.transform.GetChild(i).gameObject);
        }
    }

    public void LoadImg(Image img, string url)
    {
        StartCoroutine(LoadImg2(img, url));
    }

    IEnumerator LoadImg2(Image img, string url)
    {
        string path = this.path + url.GetHashCode().ToString() + ".png";
        WWW www = null;
        Sprite m_sprite = null;
        if (File.Exists(path))
        {
            www = new WWW("file:///" + path);
            yield return www;

            Texture2D texture = www.texture;
            m_sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
            img.sprite = m_sprite;
        }
        else
        {
            www = new WWW(url);
            yield return www;

            Texture2D tex2d = www.texture;
            //将图片保存至缓存路径  
            byte[] pngData = tex2d.EncodeToPNG();
            File.WriteAllBytes(path, pngData);

            m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
            img.sprite = m_sprite;
        }
    }

    public String ChangDis(float dis)
    {

        if (0 < dis && dis < 10)
        {
            return (int)(dis * 100) + "m";
        }
        else
        {
            return (int)dis / 10 + "km";
        }
    }
}


public class ResponseBody
{

    public string address;
    public Content content;
    public int status;

}

public class Content
{
    public string address;
    public Address_Detail address_detail;
    public Point point;
}
public class Address_Detail
{
    public string city;
    public int city_code;
    public string district;
    public string province;
    public string street;
    public string street_number;
    public Address_Detail(string city, int city_code, string district, string province, string street, string street_number)
    {
        this.city = city;
        this.city_code = city_code;
        this.district = district;
        this.province = province;
        this.street = street;
        this.street_number = street_number;
    }
}
public class Point
{
    public string x;
    public string y;
    public Point(string x, string y)
    {
        this.x = x;
        this.y = y;
    }
}
